import { Component, OnInit } from '@angular/core';
import {Router} from '@angular/router';
import {HttpClient} from '@angular/common/http';

@Component({
  selector: 'app-child-person',
  templateUrl: './child-person.component.html',
  styleUrls: ['./child-person.component.css']
})
export class ChildPersonComponent implements OnInit {
  imgUrl = '../../../assets/footer/touxiang.png';
  sex = {
    boy: false,
    girl: false
  };
  gender: any;
  nick = '';
  year = [];
  math = [];
  date = [];
  //
  years = '';
  maths = '';
  dates = '';
  constructor(private router: Router, private http: HttpClient) {
  }

  ngOnInit() {
    this.http.post('/account/mg', {nick: 'baobao'}).subscribe((data) => {
      console.log(data[0]['img']);
        this.imgUrl = data[0]['img'];
      });
    this.doYear();
    this.doMath();
    this.doDate();
  }
  doChange(e) {
      const that = this;
      const fileReader = new FileReader();
      fileReader.readAsDataURL(e.target.files[0]);
      fileReader.onload = function () {
        const there = this;
        console.log(this);
        console.log(there);
        that.imgUrl = this.result;
      };
    }
  doUpdate() {
    this.router.navigate(['/myOrder/update']);
  }
  doBoy(e) {
    this.sex.boy = !this.sex.boy;
  }
  doGirl(e) {
    this.sex.girl = !this.sex.girl;
  }
  // 年
  doYear () {
    for (let i = 1970; i < 2001; i++) {
      this.year.push(i);
    }
  }
  // 月
  doMath() {
    for (let i = 1; i < 13; i++) {
      this.math.push(i);
    }
  }
  // 日
  doDate() {
    for (let i = 1; i < 31; i++) {
      this.date.push(i);
    }
  }
  // 赋值;
  changeYear(e) {
    this.years = e.target.value;
  }
  changeMath(e) {
    this.maths = e.target.value;
  }
  changeDate(e) {
    this.dates = e.target.value;
  }
  doBtn() {
    if (this.years !== '' && this.maths !== '' && this.dates !== '') {
      if (this.sex.girl) {
        this.gender = '女';
      } else {
        this.gender = '男';
      }
      this.http.post('/account/update', {img: this.imgUrl, nick: this.nick, year: this.years, math: this.maths, date: this.dates, sex: this.gender}).subscribe((data) => {
        console.log(data);
      })
    }
  }
}
